<template>
  <div>
    <!-- 放置一个弹层 用来编辑新增节点 -->
    <el-dialog
      :title="isEdit ? '编辑' : '新增权限点'"
      :visible.sync="showDialog"
      @close="btnCancel"
    >
      <!-- 表单 -->
      <el-form
        ref="perForm"
        :model="formData"
        :rules="rules"
        label-width="120px"
      >
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="formData.name" style="width:90%" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="formData.code" style="width:90%" />
        </el-form-item>
        <el-form-item label="权限描述">
          <el-input v-model="formData.description" style="width:90%" />
        </el-form-item>
        <el-form-item label="开启">
          <el-switch
            v-model="formData.enVisible"
            active-value="1"
            inactive-value="0"
          />
        </el-form-item>
      </el-form>
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" type="primary" @click="encel">确定</el-button>
          <el-button size="small" @click="cencel">取消</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
export default {
  props: {
    isEdit: {
      type: Boolean,
      default: false
    },
    PermissionList: {
      type: Array,
      default: () => []
    }
  },
  data () {
    const validName = (rule, value, callback) => {
      let existNameList = this.PermissionList.filter(
        item => item.pid === this.formData.pid
      ).map(item => item.name) // 找到当前项的所有子级的名字返回成数组
      if (this.isEdit) {
        existNameList = this.PermissionList.filter(
          item => item.pid === this.formData.pid && item.id !== this.formData.id
        ).map(item => item.name !== value) // 找到当前项的所有子级的名字返回成数组
      }

      existNameList.includes(value) // 看当前输入的name值在不在
        ? callback(new Error(`名字${value}已被占用`))
        : callback()
    }
    const validCode = (rule, value, callback) => {
      let existCodeList = this.PermissionList.map(item => item.code)
      if (this.isEdit) {
        console.log(1)
        existCodeList = this.PermissionList.filter(
          item => item.id !== this.formData.id
        ).map(item => item.code)
      }
      existCodeList.includes(value)
        ? callback(new Error(`权限${value}已存在`))
        : callback()
    }

    return {
      showDialog: false,
      permissionList: [], // 权限管理列表数据
      formData: {
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        enVisible: '0', // 开启
        pid: '', // 添加到哪个节点下
        type: '' // 类型吖
      },
      rules: {
        name: [
          { required: true, message: '权限名称不能为空', trigger: 'blur' },
          { validator: validName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '权限标识不能为空', trigger: 'blur' },
          { validator: validCode, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    //   弹窗关闭事件
    btnCancel () {
      // 问题:先编辑,this.formData里很多key+value(包含id和_id值)
      // 你再点击新增,输入框的值只会改变formData里name等
      // formData对象里还有id和_id,调用新增接口给后台
      // 导致后台id重复从而导致后台报错

      // 下面这个置空,它只会影响标签里使用的相关的v-model属性值
      // id和_id的值还在(点击新增network里)
      this.$refs.perForm.resetFields() // 可置空报错提示

      // 还需自行清空
      // 拿到上面的data选项,再次调用data函数,返回带初始值的对象
      // 给组件运行时的formData变量,冲掉
      this.formData = this.$options.data().formData
    },
    //  确定事件
    encel () {
      this.$refs.perForm.validate(valid => {
        if (valid) {
          this.$emit('addPerFn', { ...this.formData })
          this.showDialog = false
        }
      })
    },
    // 取消事件
    cencel () {
      this.showDialog = false
    }
  }
}
</script>

<style lang="less" scoped></style>
